//
//  WNCycleViewCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/9/1.
//

import UIKit

class WNCycleViewCell: UICollectionViewCell {
    
    lazy var bookImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "placeholder_cover_ver"))
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(2)
        image.clipsToBounds = true
        return image
    }()
    
    lazy var disLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 9)
        label.textColor = .white
        label.backgroundColor = .init(named: "red_FE95B4")
        label.textAlignment = .center
        label.isHidden = true

        return label
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .black
        return label
    }()
    
    lazy var desLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "gray_999999")
        label.numberOfLines = 5
        return label
    }()
    
    lazy var readBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("閱讀", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 14)
        btn.backgroundColor = .init(named: "red_FE95B4")
        btn.isUserInteractionEnabled = false
        btn.wn_cornerWithBorder(4)
        return btn
    }()
    
    var model: BookDetailModel? {
        didSet {
            if let m = model {
                bookImage.wn_setupImg(m.coverUrl, placeHolder: "placeholder_cover_ver")
                nameLabel.text = m.name
                desLabel.text = m.des
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(bookImage)
        bookImage.addSubview(disLabel)
        addSubview(nameLabel)
        addSubview(readBtn)
        addSubview(desLabel)
        bookImage.snp.makeConstraints { (make) in
            make.top.left.bottom.equalTo(0)
            make.width.equalTo(scaleWidth(106))
        }
        disLabel.snp.makeConstraints { (make) in
            make.top.equalTo(5)
            make.right.equalTo(-5)
            make.height.equalTo(14)
            make.width.equalTo(0)
        }
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(bookImage.snp.top)
            make.left.equalTo(bookImage.snp.right).offset(8)
            make.right.equalTo(-6)
        }
        readBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(bookImage.snp.bottom)
            make.left.equalTo(bookImage.snp.right).offset(8)
            make.size.equalTo(CGSize.init(width: 60, height: 30))
        }
        desLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.left)
            make.top.equalTo(nameLabel.snp.bottom).offset(4)
            make.bottom.equalTo(readBtn.snp.top).offset(-8)
            make.right.equalTo(-6)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
